<?php

/**
 * 关键词管理logic
 */

namespace Member\Logic;

class KeywordsLogic extends BaseLogic {
    
	protected $_auto = array(
	    array('uid',UID),
	);
	
	public function getKeyword($map){
		return $this->where($map)->find();
	}
	
	/**
	 * 检查关键词是否存在重复
	 * @param $keywords 关键词
	 * @param $uid  用户ID
	 * @param $relation 关联的模块
	 * @return false 重复，true 不重复
	 */
	public function checkRepeat($keywords,$uid = UID,$relation = ''){
				
		$keywords_arr = explode(" ", $keywords);
		foreach ($keywords_arr as $k=>$v){
		    $keywords_arr[$k] = trim($v);
		}
		$map = array('uid'=>$uid,'keywords'=>array('IN',$keywords_arr));
		
		$res = $this->where($map)->find();
		
		if(!empty($res)){
		    return false;
		}
		
		return true;
	}
	
	/**
	 * 获取某个模块下面的全部的关键词信息以及图文消息
	 */
	public function moudleKeywords($moudle){
		
	    $keywords = $this->where(array('msgtype'=>$moudle,'uid'=>UID,'msgid'=>array('NEQ','0')))->select();
	    //由于都是一个关键词显示多个页面
	    if(empty($keywords)){
	        return array();
	    }
	    
	    $keyword = $keywords[0];
	    
	    $newsmsgLogic = D('Newsmsg','Logic');
	    $map = array('uid'=>UID,'id'=>$keyword['msgid']);
	    $newsmsg = $newsmsgLogic->where($map)->find();
	    
	    $newsmsg['configs'] = serializeMysql( $newsmsg['configs'],1);
	    
	    $allkeywords = array();
	    foreach ($keywords as $v){
	    	$allkeywords[] = $v['keywords'];
	    }
	    
	    $data = array();
	    //拼接数据
	    $data['msgid']			= $newsmsg['id'];
	    $data['keywords'] 		= implode(' ', $allkeywords);
	    $data['matchingtype']	= $keywords[0]['matchingtype'];
	    $data['title']			= $newsmsg['configs']['Title'];
	    $data['imgpath']		= $newsmsg['configs']['ImgPath'];
	    
	    return $data;
	    
	}
	
	/**
	 * 获取用户所有设置过的关键词
	 */
	public function allKeywords($uid = UID){
		
		$map = array(
			'uid'	=>	$uid,
		);
		$allData = $this->where($map)->select();
		if (!is_array($allData) || empty($allData)) {
			return false;	
		}
		$temp = "";
		foreach ($allData as $key => $value) {
			$temp .= " ".$value['keywords'];			
		}
		if (empty($temp)) return false;
		$array = explode(" ", trim($temp));
		foreach ($array as $k => $v){
			$allKeyword[] = trim($v);
		}
		return $allKeyword;
	}
	/**
	 * (non-PHPdoc)
	 * @see \Member\Logic\BaseLogic::update()
	 */
	public function update($data = array()){
		if(empty($data)){
			/* 获取文章数据 */
			$data = $this->create();
		}
		
		
		$data['uid'] = UID;
		
		if($data === false){
			return false;
		}
		
		/* 添加或更新数据 */
		if(empty($data['id'])){//新增数据
			$data['id'] = null;
			$id = $this->add($data);
			if(!$id){
				$this->error = '新增详细内容失败！';
				return false;
			}
		} else { //更新数据
			$status = $this->save($data);
			if(false === $status){
				$this->error = '更新详细内容失败！';
				return false;
			}
		}
		
		return true;
	}
	
	
	/**
	 * (non-PHPdoc)
	 * @see \Member\Logic\BaseLogic::autoSave()
	 */
	public function autoSave($id = 0){
		 
	}
	
	public function saveData($data){
		
		$data['matchingtype'] = $data['matchingtype'] == 1 ? 1 : 0;
		
		if($data['msgtype'] == 'text'){
			$textmsgLogic = D('Textmsg','Logic');
			$data['msgid'] = $textmsgLogic->saveData($data['text']);
		}
		
		
		
	    $map = array(
	        'uid'=>UID,
	    	'keywords'=>$data['keywords'],
	    );
	    //查找之前是否存在相关数据
	    $keyword = $this->where($map)->find();
	    //如果之前存在就更新
		if($data['id'] > 0){
			if($keyword['id'] > 0 && $keyword['id'] != $data['id']){
				$this->error = '关键词已经存在,请修改后重新提交';
				return false;
			}else{
				$this->where(array('id'=>$data['id']))->save($data);
				return true;
			}
		}else{
			if($keyword['id'] > 0){
				$this->error = '关键词已经存在,请修改后重新提交';
				return false;
			}else{
			
				$data['id'] = null;
				return $this->add($data);
			}
		}
	    
	    
	}
}